<?php

/**
 * Copyright              [HD框架] (C)2011-2012 houdunwang ，Inc. 
 * Encoding               UTF-8 
 * Version                $Id: configControl      2012-8-23 10:31:22
 * @author                向军
 * Link                   http://www.houdunwang.com       
 * E-mail                 houdunwangxj@gmail.com
 */
class configControl extends rbacControl {

	function index() {
        $this->setconfig();
    }
	
    //编辑配置
    function setconfig() {
        $db = M("config");
        $tid = $db->group("tid")->field("tid")->all();
        $config = array();
        if ($tid) {
            foreach ($tid as $t) {
                $data = $db->where("tid={$t['tid']}")->order("tid asc,id asc")->all();
                $data = format_config($data);
                $config[$t['tid']] = $data;
            }
        }
		//p($config);
        $this->assign("config", $config);
        $this->display("setconfig");
    }
	
 function set_config_save() {
        $db = M("config");
        $data = $_POST;
        $data['upload_ext'] = explode(",", $_POST['upload_ext']);
        $file_data = "<?php \n if(!defined('PATH_HD'))exit;\n return " . var_export($data,true) . ";?>";
        if (!file_put_contents("./config/base.php", $file_data)) {
            $this->error("修改配置文件失败");
        }
        foreach ($_POST as $name => $v) {
            $db->where(array("name" => $name))->save(array("value" => $v));
        }
        $this->success("修改配置文件成功");
    }

	
    //显示配置组
    function type() {
        $db = M("config_type");
        $type = $db->all();
        $this->assign("type", $type);
        $this->display();
    }

    //添加配置组
    function add_type_show() {
        $this->display();
    }
	
	//编辑配置组
	function edit_type_show(){
		$tid=$_GET["tid"];
		$db = M("config_type");
        $type = $db->where("tid=$tid")->all();
		//p($type);
        $this->assign("type", $type);
        $this->display();
	}
	
	//编辑配置组显示
	function edit_type(){
	//p($_POST);
		$db = M("config_type");
        if ($db->save()) {
            $this->success("修改配置组成功", "type");
        } else {
            $this->error("修改配置组失败", "type");
        }
	}
	
	//删除配置组
	function del_type(){
		$tid=$_GET["tid"];
		$db = M("config_type");
        if ($db->where("tid=$tid")->del()) {
            $this->success("删除配置组成功");
        } else {
            $this->error("删除配置组失败");
        }
	
	}
	
	
    //添加类型
    function add_type() {
        $db = M("config_type");
        if ($db->add()) {
            $this->success("添加配置类型成功", "type");
        } else {
            $this->error("添加配置类型失败", "type");
        }
    }

    //配置项管理config.tid  asc,
    function config() {
        $db = K("configView");
        $where = isset($_GET['tid']) ? "config.tid = '" . $_GET['tid'] . "'" : '';
        $config = $db->where($where)->order("id asc")->all();
        $this->assign("config", $config);
        $this->display();
    }
	//删除配置项
	function del_config(){
		$id=$_GET["id"];

		$db = M("config");
        if ($db->where("id=$id")->del()) {
            $this->success("删除配置项成功");
        } else {
            $this->error("删除配置项失败");
        }
	
	}
	//显示编辑配置项
	function edit_config_show(){
		$id=$_GET["id"];
		$db = M("config");
        $row = $db->where("id=$id")->all();
		foreach($row as $v){
			$row=$v;
		}
		//p($row);
		$db1 = M("config_type");
        $type = $db1->all();
        $this->assign("type", $type);
        $this->assign("row",$row);
        $this->display();
	}
	//编辑配置项
	function edit_config(){
		
		$db = M("config");
        if ($db->save()) {
            $this->success("修改配置项成功", "config");
        } else {
            $this->error("修改配置项失败", "config");
        }
	
	}
    //显示添加配置项
    function add_config_show() {
        $db = M("config_type");
        $this->assign("config_type", $db->all());
        $this->display();
    }

    //添加配置项
    function add_config() {
        $db = M("config");
        if ($db->add()) {
            $this->success("添加配置成功");
        } else {
            $this->error("添加配置失败");
        }
    }

}

?>
